package cn.edu.neusoft.demo.hospital2.common;

import java.util.List;

/**
 * 返回结果数据工具类
 */
public class Result{
    public static final String SUCCESS = "操作成功。";
    public static final String FAILURE = "操作失败。";
    private int code;
    private String msg;
    private long count;
    private Object data;
    private String token;

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public long getCount() {
        return count;
    }

    public void setCount(long count) {
        this.count = count;
    }

    public static Result ok() {
        return Result.ok(SUCCESS);
    }

    public static Result ok(String msg) {
        return Result.ok(msg, null);
    }

    public static Result ok(Object data) {
        return Result.ok(SUCCESS, data);
    }

    public static Result ok(String msg, Object data) {
        if(data instanceof List)
            return Result.ok(msg,data,((List) data).size());
        else
        {
            Result result = new Result();
            result.setCode(0);
            result.setMsg(msg);
            result.setData(data);
            return result;
        }
    }
    public static Result ok(String msg, Object data,String token) {
        if(data instanceof List)
            return Result.ok(msg,data,((List) data).size());
        else
        {
            Result result = new Result();
            result.setCode(0);
            result.setMsg(msg);
            result.setData(data);
            result.setToken(token);
            return result;
        }
    }

    //用于分页返回结果
    public static Result ok(String msg, Object data,int count) {
        Result result = new Result();
        result.setCode(0);
        result.setMsg(msg);
        result.setData(data);
        result.setCount(count);
        return result;
    }

    public static Result fail(String msg) {
        Result result = new Result();
        result.setMsg(FAILURE+msg);
        result.setCode(-1);
        return result;
    }
    public static Result fail() {
        return Result.fail("");
    }

    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
    public Object getData() {
        return data;
    }
    public void setData(Object data) {
        this.data = data;
    }
}


